package cn.itsource.system.service.impl;

import cn.itsource.base.service.impl.BaseServiceImpl;
import cn.itsource.base.util.ErrorCode;
import cn.itsource.base.util.PageList;
import cn.itsource.global.exception.BusinessException;
import cn.itsource.system.domain.Menu;
import cn.itsource.system.domain.Role;
import cn.itsource.system.domain.dto.RoleDto;
import cn.itsource.system.mapper.RoleMapper;
import cn.itsource.system.query.RoleQuery;
import cn.itsource.system.service.IMenuService;
import cn.itsource.system.service.IRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private IMenuService iMenuService;

    @Override
    @Transactional
    public void save(RoleDto role) {
        //查询sn是否已经存在
       Role role1 =  roleMapper.selectBySn(role.getSn());
       if(role1 != null){
           throw new BusinessException(ErrorCode.BUSINESS_ROLE_REPEAT_ERROR.getCode());
       }
        Role role2 = new Role();
        BeanUtils.copyProperties(role,role2);
        super.save(role2);
       //还要保存  角色和菜单关系
        roleMapper.saveRoleAndMenu(role2.getId(),role.getMenus());
        //还要保存  角色和权限关系
        roleMapper.saveRoleAndPermission(role2.getId(),role.getAuthoritys());
    }

    @Override
    public void update(RoleDto role) {
        Role role2 = new Role();
        BeanUtils.copyProperties(role,role2);
        //查询除自己之外是否存在相同的SN
        Role role1 = roleMapper.selectRole(role2);
        if(role1 != null){
            throw new BusinessException(ErrorCode.BUSINESS_ROLE_REPEAT_ERROR.getCode());
        }
        super.update(role2);
    }

    @Override
    public List<Long> getPermission(Long roleId) {
        return roleMapper.getPermissionByRoleId(roleId);
    }

    /**
     * 查询的是当前角色的菜单
     * @param roleId
     * @return
     */
    @Override
    public List<List<Long>> getMenu(Long roleId) {
        List<Long> menuIds =  roleMapper.getMenuByRoleId(roleId);
        List<List<Long>> lists = new ArrayList<>();
        List<Menu> menus = iMenuService.selectMenu(menuIds);
        for (int i = 0; i < menus.size(); i++) {
            List<Long> list = new ArrayList<>();
            Menu menu = menus.get(i);
            list.add(menu.getParentId());
            list.add(menu.getId());
            lists.add(list);
        }
        return lists;
    }

    @Override
    public PageList<Role> queryPage(RoleQuery query) {
        if (query.getCurrentPage() < 1) {
            throw new RuntimeException("");
        }
        //分页查询
        List<Role> roles = super.query(query);
        //创建分页接收对象
        PageList<Role> pageList = new PageList();
        //设置分页数据
        pageList.setData(roles);
        //设置分页总条数
        pageList.setCount(roleMapper.queryCount(query));
        return pageList;
    }
}
